<?php

function generate_password() {
 $chars = "abcdefghijkmnopqrstuvwxyz023456789";
 srand((double)microtime()*1000000);
 $i = 0;
 $pass = '' ;
 while ($i <= 7) {
     $num = rand() % 33;
     $tmp = substr($chars, $num, 1);
     $pass = $pass . $tmp;
     $i++;
    }
 return $pass;
}

function generate_password_for($user_id) {
    $pass = generate_password();
    update_password_for($user_id, $pass);
    return $pass;
}

function update_password_for($user_id, $pass) {
    $p =  escape_data($pass);
    $query = "UPDATE users SET pass=SHA('$p') WHERE user_id=$user_id";	
    $result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
}